package steampunk.geom;

import java.awt.geom.Rectangle2D;

import steampunk.art.Art;
import steampunk.art.RectArt;
import steampunk.utils.Mutil;

public class RectGeom extends BaseGeom {
	
	private double width = 0;
	private double height = 0;

	public RectGeom(int w, int h) {
		this.width = w;
		this.height = h;
	}
	public RectGeom(int x, int y, int w, int h) {
		this(w, h);
		moveTo(x, y);
	}
	
	public double getWidth() {
		return width;
	}
	public double getHeight() {
		return height;
	}
	public int iw() {
		return (int)width;
	}
	public int ih() {
		return (int)height;
	}
	public void setWidth(double d) {
		this.width = d;
	}
	public void setHeight(double d) {
		this.height = d;
	}
	
	public double right() {
		return getX() + width;
	}
	public double bottom() {
		return getY() + height;
	}
	public void right(double d) {
		setX(d - width);
	}
	public void bottom(double d) {
		setY(d - height);
	}


	private final Rectangle2D.Double interrect = new Rectangle2D.Double(0, 0, 1, 1);
	@Override
	public boolean containsPoint(double x, double y) {
		synchronized (interrect) {
			interrect.setRect(getX(), getY(), width, height);
			return interrect.contains(x, y);
		}
	}

	@Override
	public boolean intersects(Intersectable other) {
		return other.intersectsRect(ix(), iy(), iw(), ih());
	}

	@Override
	public boolean intersectsCircle(double x, double y, double radius) {
		return Mutil.intRectCircle(getX(), getY(), getWidth(), getHeight(), x, y, radius);
	}

	@Override
	public boolean intersectsLine(double x1, double y1, double x2, double y2) {
		synchronized (interrect) {
			interrect.setRect(getX(), getY(), width, height);
			return interrect.intersectsLine(x1, y1, x2, y2);
		}
	}

	@Override
	public boolean intersectsRect(int x, int y, int w, int h) {
		synchronized (interrect) {
			interrect.setRect(getX(), getY(), width, height);
			return interrect.intersects(x, y, w, h);
		}
	}

	@Override
	public Art getWireArt() {
		return new RectArt(this);
	}
	@Override
	public double left() {
		return getX();
	}
	@Override
	public void left(double d) {
		setX(d);
	}
	@Override
	public double top() {
		return getY();
	}
	@Override
	public void top(double d) {
		setY(d);
	}
	
}
